import { Provide, Inject, Config } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { User } from '../entity/user';
import { MidwayHttpError } from '@midwayjs/core';
import { CacheManager } from '@midwayjs/cache';
import { Context } from '@midwayjs/koa';
import { Type } from '../entity/type';

@Provide()
export class TypeService {
  @InjectEntityModel(User)
  userModel: Repository<User>;

  @InjectEntityModel(Type)
  typeModel: Repository<Type>;

  @Config('jwt')
  config: any;

  @Inject()
  cacheManager: CacheManager;

  @Inject()
  ctx: Context;

  async finds() {
    const result: any = await this.typeModel.find();

    if (!result) {
      throw new MidwayHttpError('没有查到文章类型!', 500);
    }

    return {
      code: 200,
      data: result,
      message: '查询文章类型成功!',
    };
  }
}
